package com.war.dao.warehousing;

import com.war.pojo.system.Supplier;
import com.war.pojo.system.User;
import com.war.pojo.system.Warehouse;
import com.war.pojo.warehousing.PurchaseInfo;
import com.war.pojo.warehousing.ReturnInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PurchaseDao {
    //查询仓库
    @Select("select * from warehousing_purchase where status=1")
    List<PurchaseInfo> selectAll();

    //根据产品编号查询采购记录
    @Select("select * from warehousing_purchase where wareHousingId=#{wareHousingId}")
    PurchaseInfo selectById(PurchaseInfo purchaseInfo);

    //查询供应商名字
    @Select("select distinct(name) from sys_supplier")
    List<Supplier> selectSupplierName();

    //查询供应商电话
    @Select("select tel from sys_supplier where name=#{name}")
    String getSupplierTel(Supplier supplier);

    //查询仓库名称
    @Select("select distinct(name) from sys_warehouse")
    List<Warehouse> selectStorageName();

    //查询仓库的管理员名称
    @Select("select manager from sys_warehouse where name=#{name}")
    String getStorageManager(Warehouse warehouse);

    //查询采购员名字
    @Select("select distinct(name) from sys_user where identity='buyer' ")
    List<User> selectBuyerName();

    //修改采购信息
    @Update("update warehousing_purchase set productName=#{productName},supplierName=#{supplierName}, supplierTel=#{supplierTel},buyer=#{buyer}," +
            "storageDate=#{storageDate},productionCompany=#{productionCompany},unitPrice=#{unitPrice},storageNum=#{storageNum},unit=#{unit}," +
            "price=#{price},storageName=#{storageName},storageManager=#{storageManager},remarks=#{remarks} where wareHousingId=#{wareHousingId}")
    int editPurchase(PurchaseInfo purchaseInfo);

    //删除采购入库的记录
    @Update("update warehousing_purchase set status=0 where wareHousingId=#{wareHousingId}")
    int delPurchase(PurchaseInfo purchaseInfo);

    //添加采购入库的记录
    @Insert("insert into warehousing_purchase(productName,supplierName,supplierTel,buyer,storageDate,productionCompany,unitPrice,storageNum,unit," +
            "price,storageName,storageManager,remarks,status) " +
            "value(#{productName},#{supplierName},#{supplierTel},#{buyer},#{storageDate},#{productionCompany},#{unitPrice},#{storageNum}," +
            "#{unit},#{price},#{storageName},#{storageManager},#{remarks},1)")
    int insertPurchase(PurchaseInfo purchaseInfo);

    //退货，添加至退货记录中
    @Insert("insert into warehousing_return(wareHousingId,returnDate,returnNum,returnReason,returnMoney,status) " +
            " value(#{wareHousingId},#{returnDate},#{returnNum},#{returnReason},#{returnMoney},1)")
    int purchaseReturn(ReturnInfo returnInfo);

    //一系列的查询
    //根据产品名查询
    @Select("SELECT * FROM warehousing_purchase where productName=#{conValue} and status=1")
    List<PurchaseInfo> selectByProductName(PurchaseInfo purchaseInfo);

    //根据供应商查询
    @Select("SELECT * FROM warehousing_purchase where supplierName=#{conValue} and status=1")
    List<PurchaseInfo> selectBySupplierName(PurchaseInfo purchaseInfo);

    //根据采购员查询
    @Select("SELECT * FROM warehousing_purchase where buyer=#{conValue} and status=1")
    List<PurchaseInfo> selectByBuyer(PurchaseInfo purchaseInfo);

    //根据入库日期范围查询查询
    @Select("select * from warehousing_purchase where storageDate between #{startDate} and #{endDate} AND status = 1\n" +
            "ORDER BY STR_TO_DATE(storageDate, '%Y-%m-%d')")
    List<PurchaseInfo> selectByDate(PurchaseInfo purchaseInfo);

    //根据仓库名称查询
    @Select("SELECT * FROM warehousing_purchase where storageName=#{conValue}  and status=1")
    List<PurchaseInfo> selectByStorageName(PurchaseInfo purchaseInfo);

    //根据仓库管理员查询
    @Select("SELECT * FROM warehousing_purchase where storageManager=#{conValue}  and status=1")
    List<PurchaseInfo> selectByStorageManager(PurchaseInfo purchaseInfo);

    //查询仓库的最大容量
    @Select("SELECT number FROM sys_warehouse where name=#{storageName}")
    int storageNumNow(PurchaseInfo purchaseInfo);

        //批量删除
    @Delete("delete FROM warehousing_purchase  where wareHousingId=#{wareHousingId}")
    int delAll(PurchaseInfo purchaseInfo);

    //入库报表
    @Select("SELECT productName,storageNum FROM warehousing_purchase")
    List<PurchaseInfo> in_num_data();

}
